<template>
  <div>
    <h1 style="font-size:30px;">注册</h1>

    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item style="margin-left:5%;margin-top:20px;" label="用户名" prop="UserName">
        <el-input
          type="text"
          v-model="ruleForm.UserName"
          autocomplete="off"
          style="width:70%"
          @blur="checkUName()"
        ></el-input>
      </el-form-item>
      <el-form-item style="margin-left:5%;margin-top:20px;" label="账号" prop="UserAccount">
        <el-input
          type="text"
          v-model="ruleForm.UserAccount"
          autocomplete="off"
          style="width:70%"
          @blur="checkUAccount()"
        ></el-input>
      </el-form-item>

      <el-form-item style="margin-left:5%;margin-top:20px;" label="密码" prop="UserPwd">
        <el-input
          type="password"
          v-model="ruleForm.UserPwd"
          autocomplete="off"
          style="width:70%"
          @blur="checkUPwd()"
        ></el-input>
      </el-form-item>
      <el-form-item style="margin-left:5%;margin-top:20px;" label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
          style="width:70%"
          @blur="checkUPwd2()"
        ></el-input>
      </el-form-item>

      <el-form-item style="margin-left:5%;margin-top:20px;" label="电话号码" prop="PhoneNum">
        <el-input
          type="text"
          v-model="ruleForm.PhoneNum"
          autocomplete="off"
          maxlength="11"
          style="width:40%"
          @blur="checkUPhone()"
        ></el-input>
        <el-button style="margin-left:3%;" @click="GetMA()">获取验证码</el-button>
      </el-form-item>

      <el-form-item style="margin-left:5%;margin-top:20px;" label="验证码" prop="CheckMA">
        <el-input
          type="text"
          v-model="ruleForm.CheckMA"
          autocomplete="off"
          style="width:70%"
          @blur="checkUMA()"
        ></el-input>
      </el-form-item>

      <el-form-item style="margin-top:20px;">
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>>
<script>
export default {
  data() {
    var validateComfirmPwd = (rule, value, callback) => {
      if (this.ruleForm.UserPwd !== value) {
        callback(new Error("两次输入的密码不一致！"));
      } else {
        callback();
      }
    };

    return {
      MA: "",
      ruleForm: {
        UserName: "",
        UserAccount: "",
        UserPwd: "",
        checkPass: "",
        PhoneNum: "",
        CheckMA: ""
      }
    };
  },
  methods: {
    GetMA() {
      this.Ajax.get("api/User/GetMa?PhoneNum=" + this.ruleForm.PhoneNum).then(
        M => {
          this.MA = M.data;
          console.log(M.data);
        }
      );
    },
    checkUName() {
      var res1 = /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/;
      if (this.ruleForm.UserName == "" || this.ruleForm.UserName == null) {
        this.$message({
          showClose: true,
          message: "请输入姓名",
          type: "error"
        });
      } else if (res1.test(this.ruleForm.UserName) == false) {
        this.$message({
          showClose: true,
          message: "姓名不支持特殊字符",
          type: "error"
        });
      }
    },
    checkUAccount() {
      var res1 = /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/;
      if (
        this.ruleForm.UserAccount == "" ||
        this.ruleForm.UserAccount == null
      ) {
        this.$message({
          showClose: true,
          message: "请输入账号",
          type: "error"
        });
      } else if (res1.test(this.ruleForm.UserAccount) == false) {
        this.$message({
          showClose: true,
          message: "账号不支持特殊字符",
          type: "error"
        });
      }
    },
    checkUPwd() {
      if (this.ruleForm.UserPwd == "" || this.ruleForm.UserPwd == null) {
        this.$message({
          showClose: true,
          message: "请输入密码",
          type: "error"
        });
      }
    },
    checkUPwd2() {
      if (this.ruleForm.checkPass == "" || this.ruleForm.checkPass == null) {
        this.$message({
          showClose: true,
          message: "请确认密码",
          type: "error"
        });
      } else if (this.ruleForm.checkPass !== this.ruleForm.UserPwd) {
        this.$message({
          showClose: true,
          message: "两次输入的密码不一致",
          type: "error"
        });
      }
    },
    checkUPhone() {
      var res2 = /^((13|14|15|16|17|18)[0-9]{1}\d{8})|((166|199|198)[0-9]{1}\d{7})$/;
      if (this.ruleForm.PhoneNum == "" || this.ruleForm.PhoneNum == null) {
        this.$message({
          showClose: true,
          message: "请输入电话号码",
          type: "error"
        });
      } else if (res2.test(this.ruleForm.PhoneNum) == false) {
        this.$message({
          showClose: true,
          message: "请输入正确的电话号码格式",
          type: "error"
        });
      }
    },
    checkUMA() {
      if (this.ruleForm.CheckMA == "" || this.ruleForm.CheckMA == null) {
        this.$message({
          showClose: true,
          message: "请输入验证码",
          type: "error"
        });
      }
    },
    submitForm(formName) {
      if (
        this.ruleForm.UserName == "" ||
        this.ruleForm.UserAccount == "" ||
        this.ruleForm.UserPwd == "" ||
        this.ruleForm.checkPass == "" ||
        this.ruleForm.PhoneNum == "" ||
        this.ruleForm.CheckMA == ""
      ) {
        this.$message({
          showClose: true,
          message: "请填写完整",
          type: "error"
        });
      } else {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.ruleForm.CheckMA == this.MA) {
              this.Ajax.post("api/User/AddUser", {
                data: {
                  UserName: this.ruleForm.UserName,
                  UserAccount: this.ruleForm.UserAccount,
                  UserPwd: this.ruleForm.UserPwd,
                  PhoneNum: this.ruleForm.PhoneNum
                }
              }).then(U => {
                if (U.data == 1) {
                  this.$notify({
                    title: "成功",
                    message: "注册成功",
                    type: "success"
                  });
                  window.location.reload();
                } else if (U.data == 3) {
                  this.$notify.error({
                    title: "错误",
                    message: "账号已存在"
                  });
                } else {
                  this.$notify.error({
                    title: "错误",
                    message: "注册失败"
                  });
                }
              });
            }
          } else {
            this.$message.error("验证码错误");
            return false;
          }
        });
      }
    }
  }
};
</script>
<style>
</style>